package com.bwda.dsrs.systemservice.controller;

import com.bwda.dsrs.base.centerbase.response.CentreCutPageResponse;
import com.bwda.dsrs.base.centerbase.response.CentreListResponse;
import com.bwda.dsrs.base.centerbase.response.ResponseEntity;
import com.bwda.dsrs.base.util.CopyUtil;
import com.bwda.dsrs.systemservice.annotations.NeedRole;
import com.bwda.dsrs.systemservice.aspect.LogTypeEnum;
import com.bwda.dsrs.systemservice.aspect.Loggable;
import com.bwda.dsrs.systemservice.domain.condition.verifycode.VerifyCodeCondition;
import com.bwda.dsrs.systemservice.domain.exception.SystemServiceException;
import com.bwda.dsrs.systemservice.domain.form.verifycode.VerifyCodeCreateForm;
import com.bwda.dsrs.systemservice.domain.form.verifycode.VerifyCodeQueryForm;
import com.bwda.dsrs.systemservice.domain.form.verifycode.VerifyCodeUpdateForm;
import com.bwda.dsrs.systemservice.domain.po.verifycode.VerifyCodePo;
import com.bwda.dsrs.systemservice.domain.vo.verifycode.VerifyCodeVo;
import com.bwda.dsrs.systemservice.service.VerifyCodeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * @author yangjun
 */
@RestController
@RequestMapping("/s/verifycode")
@Api(value = "/s/verifycode", tags = "验证码记录表，仅做记录，不做验证")
public class VerifyCodeController extends BaseController {

    @Autowired
    private VerifyCodeService verifyCodeService;

    @ApiOperation(value = "查询验证码记录表，仅做记录，不做验证", notes = "查询验证码记录表，仅做记录，不做验证", httpMethod = "GET")
    @Loggable(descp = "查询验证码记录", type = LogTypeEnum.QUERY, include = "")
    @RequestMapping(value = "/query", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity<VerifyCodeVo> query(@ApiParam(value = "ID主键", required = true) @RequestParam Integer id) throws SystemServiceException {
        VerifyCodePo po = verifyCodeService.queryWithValid(id);
        VerifyCodeVo vo = CopyUtil.transfer(po, VerifyCodeVo.class);
        return getSuccessResult(vo);
    }

    @ApiOperation(value = "查询验证码记录表，仅做记录，不做验证数量", notes = "查询验证码记录表，仅做记录，不做验证数量", httpMethod = "GET")
    @Loggable(descp = "查询验证码记录数量", type = LogTypeEnum.QUERY, include = "")
    @RequestMapping(value = "/queryCount", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity<Integer> queryCount(@ModelAttribute @Valid VerifyCodeQueryForm form) throws SystemServiceException {
        VerifyCodeCondition condition = this.getConditionByQueryForm(form);
        int count = verifyCodeService.queryCount(condition);
        return getSuccessResult(count);
    }

    @ApiOperation(value = "查询验证码记录表，仅做记录，不做验证列表", notes = "查询验证码记录表，仅做记录，不做验证列表", httpMethod = "GET")
    @Loggable(descp = "查询验证码记录", type = LogTypeEnum.QUERY, include = "")
    @RequestMapping(value = "/queryList", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity<CentreListResponse<VerifyCodeVo>> queryList(@ModelAttribute @Valid VerifyCodeQueryForm form) throws SystemServiceException {
        VerifyCodeCondition condition = this.getConditionByQueryForm(form);
        List<VerifyCodePo> poList = verifyCodeService.queryList(condition);
        List<VerifyCodeVo> voList = CopyUtil.transfer(poList, VerifyCodeVo.class);
        return getSuccessResult(getListResponse(voList));
    }

    @ApiOperation(value = "查询验证码记录表，仅做记录，不做验证列表(带分页)", notes = "查询验证码记录表，仅做记录，不做验证列表(带分页)", httpMethod = "GET")
    @Loggable(descp = "查询验证码记录(带分页)", type = LogTypeEnum.QUERY, include = "")
    @RequestMapping(value = "/queryPageList", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity<CentreCutPageResponse<VerifyCodeVo>> queryPageList(@ModelAttribute @Valid VerifyCodeQueryForm form) throws SystemServiceException {
        VerifyCodeCondition condition = this.getConditionByQueryForm(form);
        List<VerifyCodeVo> voList = new ArrayList<>();
        int count = verifyCodeService.queryCount(condition);
        if (count > 0) {
            List<VerifyCodePo> poList = verifyCodeService.queryList(condition);
            voList = CopyUtil.transfer(poList, VerifyCodeVo.class);
        }
        return getSuccessResult(getPageResponse(form, count, voList));
    }
    @NeedRole("sysadminrole")
    @ApiOperation(value = "新增验证码记录表，仅做记录，不做验证", notes = "新增验证码记录表，仅做记录，不做验证", httpMethod = "POST")
    @Loggable(descp = "新增验证码", type = LogTypeEnum.CREATE, include = "")
    @RequestMapping(value = "/add", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity<VerifyCodeVo> add(@ModelAttribute @Valid VerifyCodeCreateForm form) throws SystemServiceException {
        VerifyCodePo po = CopyUtil.transfer(form, VerifyCodePo.class);
        po.setId(1);
        verifyCodeService.insert(po);
        VerifyCodeVo vo = CopyUtil.transfer(po, VerifyCodeVo.class);
        return getSuccessResult(vo);
    }
    @NeedRole("sysadminrole")
    @ApiOperation(value = "修改验证码记录表，仅做记录，不做验证", notes = "修改验证码记录表，仅做记录，不做验证", httpMethod = "POST")
    @Loggable(descp = "修改验证码", type = LogTypeEnum.UPDATE, include = "")
    @RequestMapping(value = "/update", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity update(@ModelAttribute @Valid VerifyCodeUpdateForm form) throws SystemServiceException {
        VerifyCodePo po = CopyUtil.transfer(form, VerifyCodePo.class);
        verifyCodeService.update(po);
        return getSuccessResult();
    }
    @NeedRole("sysadminrole")
    @ApiOperation(value = "删除验证码记录表，仅做记录，不做验证", notes = "删除验证码记录表，仅做记录，不做验证", httpMethod = "POST")
    @Loggable(descp = "删除验证码", type = LogTypeEnum.DELETE, include = "")
    @RequestMapping(value = "/delete", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity delete(@ApiParam(value = "ID主键", required = true) @RequestParam Integer id) throws SystemServiceException {
        verifyCodeService.delete(id);
        return getSuccessResult();
    }

    @ApiOperation(value = "查询最新验证码", notes = "查询最新验证码", httpMethod = "GET")
    @Loggable(descp = "查询最新验证码", type = LogTypeEnum.QUERY, include = "")
    @PostMapping(value = "/queryLastVerifycode")
    public ResponseEntity<VerifyCodePo> queryLastVerifycode(@RequestBody String userCode) throws SystemServiceException {
        VerifyCodePo po = verifyCodeService.queryLastVerifyCode(userCode);
        return getSuccessResult(po);
    }

    /**
     * VerifyCodeQueryForm转换为VerifyCodeCondition
     *
     * @param form
     * @return
     */
    private VerifyCodeCondition getConditionByQueryForm(VerifyCodeQueryForm form) {
        VerifyCodeCondition condition = CopyUtil.transfer(form, VerifyCodeCondition.class);
        return condition;
    }

}